package com.ht.medicaltreatment.activity

import android.content.Intent
import android.content.res.Configuration
import android.view.View
import androidx.activity.viewModels
import androidx.core.view.isGone
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.BuildAddAdapter
import com.ht.medicaltreatment.bean.PDQueryBoxBean
import com.ht.medicaltreatment.view.ProjectDialog
import com.ht.medicaltreatment.view.TypeDialog
import com.ht.medicaltreatment.view.TypeLandDialog
import com.ht.medicaltreatment.viewmodel.BuildOrderViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_build_order_layout.btnPositive
import kotlinx.android.synthetic.main.activity_build_order_layout.btnStart
import kotlinx.android.synthetic.main.activity_build_order_layout.cbAll
import kotlinx.android.synthetic.main.activity_build_order_layout.clRoot
import kotlinx.android.synthetic.main.activity_build_order_layout.etName
import kotlinx.android.synthetic.main.activity_build_order_layout.imgDelete
import kotlinx.android.synthetic.main.activity_build_order_layout.imgSampleType
import kotlinx.android.synthetic.main.activity_build_order_layout.recyclerView
import kotlinx.android.synthetic.main.activity_build_order_layout.tvCheckCount
import kotlinx.android.synthetic.main.activity_build_order_layout.tvProjectName
import kotlinx.android.synthetic.main.activity_build_order_layout.tvSampleType

//创建盘点计划
class BuildInventoryOrderActivity:BaseActivity(), View.OnClickListener, OnItemClickListener {
    override fun getLayoutId() = R.layout.activity_build_order_layout

    private var projectId = 0
    private var typeId = ""
    private var sampleBoxName = ""
    private var json = ""
    private val model by viewModels<BuildOrderViewModel>()
    private val adapter = BuildAddAdapter()

    override fun init() {
        setTitle("创建盘点计划")

        tvProjectName.setOnClickListener(this)
        tvSampleType.setOnClickListener(this)
        imgSampleType.setOnClickListener(this)
        btnStart.setOnClickListener(this)
        btnPositive.setOnClickListener(this)
        imgDelete.setOnClickListener(this)
        cbAll.setOnClickListener(this)

        ClickUtils.expandClickArea(imgDelete,30)

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
    }

    override fun isViewStup() = true

    override fun onClick(v: View?) {
        when(v){
            //项目
            tvProjectName -> {
                ProjectDialog(this,projectId).show {id,name ->
                    tvProjectName.text = name
                    projectId = id
                }
            }

            //样本类型
            tvSampleType -> {
                if (this.getResources().getConfiguration().orientation ==
                    Configuration.ORIENTATION_PORTRAIT) {
                    //竖屏
                    TypeDialog(this).show { id, name ->
                        tvSampleType.text = name
                        typeId = "$id"
                    }
                } else {
                    //横屏
                    TypeLandDialog(this).show { id, name ->
                        tvSampleType.text = name
                        typeId = "$id"
                    }
                }
            }

            cbAll -> {
                adapter.data.forEach {
                    it.isChecked = cbAll.isChecked
                }
                adapter.notifyDataSetChanged()
            }

            //删除
            imgDelete -> {
                if(adapter.data.removeIf { it.isChecked }){
                    adapter.notifyDataSetChanged()
                    showToast("已删除")
                }
            }

            //查询
            btnStart -> {
                val intent = Intent(this,InventoryQueryActivity::class.java)
                intent.putExtra("projectId",projectId)
                intent.putExtra("sampleBoxName",sampleBoxName)
                intent.putExtra("sampleCategoryId",typeId)
                intent.putExtra("json",GsonUtils.toJson(adapter.data))

                startActivityForResult(intent){
                    json = it?.getStringExtra("json")?:""
                    val list = GsonUtils.fromJson<MutableList<PDQueryBoxBean>>(json,genericType<MutableList<PDQueryBoxBean>>())
                    if(list.size > 0 && clRoot.isGone){
                        clRoot.visibility = View.VISIBLE
                        btnPositive.visibility = View.VISIBLE
                    }
                    adapter.addData(list)
                    tvCheckCount.text = "${adapter.data.size}"
                }
            }

            btnPositive -> {
                val name = etName.text.toString()
                if(name.isEmpty()){
                    showToast("请输入表单名称")
                    return
                }
                var boxIds = ""
                adapter.data.forEach {
                    if(boxIds.isEmpty()){
                        boxIds = it.id.toString()
                    }else{
                        boxIds = "$boxIds,${it.id}"
                    }
                }
                showProgress()
                model.save(name,boxIds){
                    dismissProgress()
                    setResult(RESULT_OK)
                    finish()
                }
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as PDQueryBoxBean
        item.isChecked = !item.isChecked
        adapter.notifyItemChanged(position)
        val list = adapter.data as MutableList<PDQueryBoxBean>
        val bean = list.find { !it.isChecked }
        if(bean == null){
            cbAll.isChecked = true
        }else{
            if(cbAll.isChecked){
                cbAll.isChecked = false
            }
        }
    }
}

